package com.factory.rental.controller;

import com.factory.rental.common.PageResult;
import com.factory.rental.common.PageSupport;
import com.factory.rental.common.Result;
import com.factory.rental.dto.CleaningRecordDTO;
import com.factory.rental.model.CleaningRecord;
import com.factory.rental.service.CleaningRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 清洁记录控制器
 * 提供清洁记录的CRUD操作和其他相关API
 */
@RestController
@RequestMapping("/cleaning-records")
public class CleaningRecordController {

    private final CleaningRecordService cleaningRecordService;

    @Autowired
    public CleaningRecordController(CleaningRecordService cleaningRecordService) {
        this.cleaningRecordService = cleaningRecordService;
    }

    /**
     * 创建清洁记录
     * @param cleaningRecordDTO 清洁记录DTO
     * @return 创建后的清洁记录
     */
    @PostMapping("save")
    public Result<CleaningRecord> createCleaningRecord(@RequestBody CleaningRecordDTO cleaningRecordDTO) {
        CleaningRecord savedCleaningRecord = cleaningRecordService.saveCleaningRecord(cleaningRecordDTO);
        return Result.success(savedCleaningRecord, "清洁记录创建成功");
    }

    /**
     * 根据ID获取清洁记录
     * @param id 清洁记录ID
     * @return 清洁记录
     */
    @GetMapping("/{id}")
    public Result<CleaningRecord> getCleaningRecordById(@PathVariable Long id) {
        Optional<CleaningRecord> cleaningRecord = cleaningRecordService.getCleaningRecordById(id);
        if (cleaningRecord.isPresent()) {
            return Result.success(cleaningRecord.get());
        } else {
            return Result.error(404, "未找到指定ID的清洁记录");
        }
    }

    /**
     * 根据房间ID获取清洁记录列表
     * @param roomId 房间ID
     * @return 清洁记录列表
     */
    @GetMapping("/room/{roomId}")
    public Result<List<CleaningRecord>> getCleaningRecordsByRoomId(@PathVariable Long roomId) {
        List<CleaningRecord> cleaningRecords = cleaningRecordService.getCleaningRecordsByRoomId(roomId);
        return Result.success(cleaningRecords);
    }

    /**
     * 根据清洁员ID获取清洁记录列表
     * @param cleanerId 清洁员ID
     * @return 清洁记录列表
     */
    @GetMapping("/cleaner/{cleanerId}")
    public Result<List<CleaningRecord>> getCleaningRecordsByCleanerId(@PathVariable String cleanerId) {
        List<CleaningRecord> cleaningRecords = cleaningRecordService.getCleaningRecordsByCleanerId(cleanerId);
        return Result.success(cleaningRecords);
    }

    /**
     * 根据状态获取清洁记录列表
     * @param status 清洁状态
     * @return 清洁记录列表
     */
    @GetMapping("/status/{status}")
    public Result<List<CleaningRecord>> getCleaningRecordsByStatus(@PathVariable CleaningRecord.CleaningStatus status) {
        List<CleaningRecord> cleaningRecords = cleaningRecordService.getCleaningRecordsByStatus(status);
        return Result.success(cleaningRecords);
    }

    /**
     * 获取所有清洁记录（分页）
     * @param page 页码
     * @param size 每页数量
     * @return 分页后的清洁记录列表
     */
    @GetMapping("pageList")
    public PageResult<CleaningRecord> getAllCleaningRecords(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageSupport.ofPage(page, size);
        Page<CleaningRecord> cleaningRecords = cleaningRecordService.getAllCleaningRecords(pageable);
        return PageResult.success(cleaningRecords);
    }

    /**
     * 更新清洁记录
     * @param id 清洁记录ID
     * @param cleaningRecordDTO 清洁记录DTO
     * @return 更新后的清洁记录
     */
    @PutMapping("/{id}")
    public Result<CleaningRecord> updateCleaningRecord(@PathVariable Long id, @RequestBody CleaningRecordDTO cleaningRecordDTO) {
        try {
            CleaningRecord updatedCleaningRecord = cleaningRecordService.updateCleaningRecord(id, cleaningRecordDTO);
            return Result.success(updatedCleaningRecord, "清洁记录更新成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的清洁记录");
        }
    }

    /**
     * 删除清洁记录
     * @param id 清洁记录ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteCleaningRecord(@PathVariable Long id) {
        try {
            cleaningRecordService.deleteCleaningRecord(id);
            return Result.success(null, "清洁记录删除成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的清洁记录");
        }
    }

    /**
     * 更新清洁记录状态
     * @param id 清洁记录ID
     * @param status 新的清洁状态
     * @return 更新结果
     */
    @PutMapping("/{id}/status/{status}")
    public Result<Void> updateCleaningRecordStatus(@PathVariable Long id, @PathVariable CleaningRecord.CleaningStatus status) {
        try {
            cleaningRecordService.updateCleaningRecordStatus(id, status);
            return Result.success(null, "清洁记录状态更新成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的清洁记录");
        }
    }

    /**
     * 检查清洁记录
     * @param id 清洁记录ID
     * @param score 评分
     * @param notes 检查备注
     * @return 检查结果
     */
    @PutMapping("/{id}/inspect")
    public Result<Void> inspectCleaningRecord(
            @PathVariable Long id,
            @RequestParam Integer score,
            @RequestParam(required = false) String notes) {
        try {
            cleaningRecordService.inspectCleaningRecord(id, score, notes);
            return Result.success(null, "清洁记录检查成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的清洁记录");
        }
    }
}